<?php


namespace app\general\controller;


use app\general\model\SqUser;
use think\Controller;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Db;

class Nickname extends Controller
{
   /**
    * 爬取昵称
    */
   public function getName(){
       requests::$input_encoding = 'GB2312';
       requests::$output_encoding = 'GB2312';
       //requests::set_timeout(0);
       requests::set_useragent(array(
           "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/",
           "Opera/9.80 (Android 3.2.1; Linux; Opera Tablet/ADR-1109081720; U; ja) Presto/2.8.149 Version/11.10",
           "Mozilla/5.0 (Android; Linux armv7l; rv:9.0) Gecko/20111216 Firefox/9.0 Fennec/9.0"
       ));
       $data=array();
       for($i=1;$i<43;$i++){
           $url = "https://www.jxsfybjy.com/nan/sangezi/list_74_$i.html";
           $html = requests::get($url);
           $selector = "//ul[@class='e2']/li/p/text()";
           //获取标题
           $names = selector::select($html, $selector,'xpath');
           foreach ($names as $name){
               $data[]=array(
                   "name"=>$name
               );
           }
       }
       $flag=Db::table("xys_sq_nickname")->insertAll($data);
       if($flag){
           echo "爬取完成";
       }else{
           echo "爬取失败";
       }
   }
   /**
    * 修改微信昵称
    */
   public function modifyNickName(){
       $pn=1;
       $users=(new SqUser())->where("is_robot",1)->where("create_time","=",0)->order("id desc")->limit(($pn-1)*2000,2000)->select();
       $name=Db::table("xys_sq_nickname")->select();
       foreach ($users as $u){
           $index=rand(1,count($name));
           $names=$name[$index-1]['name'];
           (new SqUser())->save(['nick_name'=>$names,'create_time'=>time()],["id"=>$u['id']]);
       }
       echo "完成";
   }
}